package com.fingard.dsp.bank.directbank.smbc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.io.FileHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;

/**
 * 三井住友
 * @version 1.0
 */
public class SMBC01Transaction extends SMBC01Base implements ITransaction {

    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //查询明细数据
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo ="";
        retAts.writeStart();
        try{
            String accountNum = p_req7516.actNum;
            Date dateStart = Format.Date8Format.parse(p_req7516.dateFrom);
            Date dateEnd = Format.Date8Format.parse(p_req7516.dateTo);
            Calendar calendar = Calendar.getInstance();
            while (!dateStart.after(dateEnd)){
                String dealedPath=getBankConfig().getValueAsString("DealedPath")+File.separator+
                        Format.Date8Format.format(dateStart)+File.separator+"MT940"+File.separator+
                        accountNum+convertCurToBank(p_req7516.actCur)+"_7516.txt";
                if(!FileHelper.existFile(dealedPath)){
                    dealedPath = getBankConfig().getValueAsString("DealedPath")+File.separator+
                            Format.Date8Format.format(dateStart)+File.separator+"MT940"+File.separator+
                            accountNum+"_7516.txt";
                    if(!FileHelper.existFile(dealedPath)){
                        calendar.setTime(dateStart);
                        calendar.add(Calendar.DAY_OF_MONTH, 1);
                        dateStart = calendar.getTime();
                        continue;
                    }
                }
                WriteBankLogLn("正在读取明细文件路径：" + dealedPath);
                WriteBankLogLn("明细文件内容：");
                String readLine = "";
                InputStreamReader isr = new InputStreamReader(new FileInputStream(dealedPath), getCharset());
                BufferedReader reader = new BufferedReader(isr);
                while ((readLine=reader.readLine())!=null) {
                    WriteBankLogLn(readLine);
                    //账号 | 币种 | 交易日期| 交易时间| 计息日期 | 交易金额 |借贷标识| 账户余额 |对方账号|
//                    对方户名 | 对方银行 |用途| 流水号|
                    String[] transDetail = readLine.split("\\|");
                    if(transDetail.length>12){
                        RetAtsTransDetail retDetail = new RetAtsTransDetail();
                        retDetail.actArea=p_req7516.actArea;
                        retDetail.actNum = transDetail[0].trim();
                        retDetail.cur =convertCurToATS(transDetail[1].trim());
                        retDetail.tranDate = transDetail[2].trim();
                        retDetail.tranTime = transDetail[3].trim();
                        retDetail.valueDate = transDetail[4].trim();
                        retDetail.tranAmt = transDetail[5].trim();
                        retDetail.recAccNum = transDetail[8].trim();
                        retDetail.cdFlag = "C".equals(transDetail[6].trim())?"2":"1";
                        retDetail.accBal = transDetail[7].trim();
                        retDetail.recAccName = transDetail[9].trim();
                        retDetail.recAccBank = transDetail[10].trim();
                        String[] transArray=transDetail[11].split("##");
                        if(transArray.length>1){
                            if(transArray[0].trim().length()>256){
                                retDetail.usage = transArray[0].trim().substring(0,256);
                                retDetail.comment = transArray[0].trim().substring(256)+" "+transArray[1];
                            }else {
                                retDetail.usage = transArray[0].trim();
                                retDetail.comment = transArray[1];
                            }
                        }else {
                            if(transArray[0].trim().length()>256){
                                retDetail.usage = transArray[0].trim().substring(0,256);
                                retDetail.comment = transArray[0].trim().substring(256);
                            }else {
                                retDetail.usage = transArray[0].trim();
                            }
                        }
                        //retDetail.askNumber=transDetail[12].trim();
                        retAts.writeRetDetail(retDetail);
                    }
                }
                reader.close();
                calendar.setTime(dateStart);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateStart = calendar.getTime();
            }
        }catch (Exception e){
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询历史明细异常："+e);
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("明细为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        return null;
    }
}
